#region Using Statements

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace _1942
{
    /// <summary>
    /// This game component implements a manager for the boss.
    /// </summary>
    public class BossManager : DrawableGameComponent
    {
        #region Variables

        // Time for a new plane
        private const int ADDBOSSTIME = 600;
        // Audio
        private AudioLibrary audio;

        protected Texture2D bossTexture;
        protected TimeSpan elapsedTime = TimeSpan.Zero;
        protected List<Boss> boss;
			   
        private Color[] texturesData;

        #endregion

        #region Constructors

        public BossManager(Game game, ref Texture2D theTexture)
            : base(game)
        {
            boss = new List<Boss>();
            bossTexture = theTexture;

            // Get the audio library
            audio = (AudioLibrary)
                Game.Services.GetService(typeof(AudioLibrary));

            Rectangle frame = new Rectangle();
            frame.X = 394;
            frame.Y = 591;
            frame.Width = 212;
            frame.Height = 170;
            //frame.X = 394;
            //frame.Y = 591;
            //frame.Width = 212;
            //frame.Height = 180;
            texturesData = new Color[frame.Width * frame.Height];
            theTexture.GetData<Color>(0, frame, texturesData, 0, frame.Width * frame.Height);

        }

        #endregion

        #region Methods

        /// <summary>
        /// Allows the game component to perform any initialization it needs to 
        /// before starting to run.  This is where it can query for any required
        /// services and load content.
        /// </summary>
        public override void Initialize()
        {
            Start();

            base.Initialize();
        }

        /// <summary>
        /// Start the Boss attack
        /// </summary>
        public void Start()
        {
            // Initialize a counter
            elapsedTime = TimeSpan.Zero;
        }

        private void CheckforNewBoss(GameTime gameTime)
        {
            // Add a boss each ADDBOSSTIME
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime > TimeSpan.FromMilliseconds(ADDBOSSTIME))
            {
                elapsedTime -= TimeSpan.FromMilliseconds(ADDBOSSTIME);
                AddNewBoss();
            }
        }

        //Add the boss
        public void addBoss()
        {
            AddNewBoss();
        }

        /// <summary>
        /// Add a new boss-plane in the scene
        /// </summary>
        public Boss AddNewBoss()
        {
            Boss newBoss = new Boss(Game, ref bossTexture);
            newBoss.Initialize();
            boss.Add(newBoss);
            return newBoss;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            
            for (int i = 0; i < boss.Count; i++)
            {
                boss[i].Update(gameTime);
            }

           base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component draw your content in game screen
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            for (int i = 0; i < boss.Count; i++)
            {
                boss[i].Draw(gameTime);
            }

            base.Draw(gameTime);
        }

        #endregion
			
		#region Properties

        public Color[] TextureData
        {
            get { return texturesData; }
        }  

        /// All bosses in the game
        /// </summary>
        public List<Boss> AllBosses
        {
            get { return boss; }
        }

#endregion

        internal bool isDefeated()
        {
            foreach (Boss b in boss)
            {
                if(b.IsDefeated) return b.IsDefeated;
            }
            return false;
        }
    }
}